﻿using RescueApp.Data;
using RescueApp.Models;
using Rescue.Services;
using EFCoreRepository.DbContexts;
using EFCoreRepository.Extensions;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using System.Data;
using Microsoft.Extensions.DependencyInjection;
using RescueApp.Repository;



namespace RescueApp.CreateDb
{
    internal class Program
    {
        static void Main(string[] args)
        {
            const string connectionString = "Server=LAPTOP-GKEKHVVJ;Database=Todo;TrustServerCertificate=True;Integrated Security=True;";


            RescueContext db = new RescueContext(connectionString);
            RescueRepos repos = new RescueRepos();
            db.Database.GetDbConnection().Open();




            //repos
            // RescueApply表
            InsertDemo(db, repos);//插入id=2
            QueryAllDemo(db, repos);//遍历
            UpdateDemo(db, repos);//修改ID=2的信息
          //DeleteDemo(db, repos);//删除ID=2的信息

            //RescueDispatch表
            InsertDemo_2(db, repos);//插入id=2
            QueryAllDemo_2(db, repos);//遍历
            UpdateDemo_2(db, repos);//修改ID=2的信息
            //DeleteDemo_2(db, repos);//删除ID=2的信息

        }


        //repos

        static void QueryAllDemo(RescueContext db, RescueRepos repos)
        {
            var srv = new RescueApplyService(db, repos);
            var todos = srv.QueryAllItems();
            foreach (var item in todos)
            {
                Console.WriteLine($"RescueID: {item.RescueID} - LicenseID: {item.LicenseID} - CarType: {item.CarType} - DriverLicenseID: {item.DriverLicenseID} - Phone: {item.Phone} - RescueLocation: {item.RescueLocation} - Destination: {item.Destination} - FillingDate: {item.FillingDate}");
            }
        }


        static void QueryAllDemo_2(RescueContext db, RescueRepos repos)
        {
            var srv = new RescueDispatchService(db, repos);
            var todos = srv.QueryAllItems();
            foreach (var item in todos)
            {
                Console.WriteLine($"DispatchID: {item.DispatchID} - RescueID: {item.RescueID} - Driver: {item.Driver} - LicenseID: {item.LicenseID} - ArrivedTime: {item.ArrivedTime} - Cause: {item.Cause} - Cost: {item.Cost}");
            }

        }

        static void InsertDemo(RescueContext db, RescueRepos repos)
       {
        var svr = new RescueApplyService(db, repos);
        RescueApply item = new RescueApply();
            item.RescueID = 2;
            item.LicenseID = "2-12345D";
            item.CarType = "car";
            item.DriverLicenseID = "520704199801021234";
            item.Phone = "22345678901";
            item.RescueLocation = "wuhan";
            item.Destination = "ccnu-9";
            item.FillingDate = DateTime.Now;
            if (svr.FindById(item.RescueID) != null)
        {
            Console.WriteLine("The Record already exists");
            return;
        }
        else
        {
            try
            {
                svr.Insert(item);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        }


        static void InsertDemo_2(RescueContext db, RescueRepos repos)
        {
            var svr = new RescueDispatchService(db, repos);
            RescueDispatch item = new RescueDispatch();
            item.DispatchID = 2;
            item.RescueID = 2;
            item.Driver = "lwr";
            item.LicenseID = "2-08042D";
            item.ArrivedTime = DateTime.Now.AddHours(1);//在现在的时间的基础上加入1小时
            item.Cause = "Test cause";
            item.Cost = 200.5f;
            if (svr.FindById(item.RescueID) != null)
            {
                Console.WriteLine("The Record already exists");
                return;
            }
            else
            {
                try
                {
                    svr.Insert(item);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        static void UpdateDemo(RescueContext db, RescueRepos repos)
        {

            var svr = new RescueApplyService(db, repos);
            int Id = 2;
            var item = svr.FindById(Id);
            if (item == null)
            {
                Console.WriteLine("The Record do not exist");
                return;
            }
            else
            {
                try
                {
                   
                    item.CarType = "bus";
                    item.RescueLocation = "ezhou";
                    svr.Update(item);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        static void UpdateDemo_2(RescueContext db, RescueRepos repos)
        {

            var svr = new RescueDispatchService(db, repos);
            int Id = 2;
            var item = svr.FindById(Id);
            if (item == null)
            {
                Console.WriteLine("The Record do not exist");
                return;
            }
            else
            {
                try
                {
                    item.Driver = "hcy";
                    item.LicenseID = "3-50207M";
                    svr.Update(item);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

      


        static void DeleteDemo(RescueContext db, RescueRepos repos)
        {
            var svr = new RescueApplyService(db, repos); ;
            int Id = 2;
            var item = svr.FindById(Id);
            if (item == null)
            {
                Console.WriteLine("The Record do not exist");
                return;
            }
            else
            {
                try
                {
                    svr.Delete(item);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }


        static void DeleteDemo_2(RescueContext db, RescueRepos repos)
        {
            var svr = new RescueDispatchService(db, repos); ;
            int Id = 2;
            var item = svr.FindById(Id);
            if (item == null)
            {
                Console.WriteLine("The Record do not exist");
                return;
            }
            else
            {
                try
                {
                    svr.Delete(item);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }


    }
}
